﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using System.ComponentModel;
using System.Collections.Generic;
namespace WPFExcelReport
{
    public partial class UIKS05 : ViewBase
    {
        public override void ManageCommand(object sender, RoutedEventArgs e)
        {
            if (!IsManage)
            {
                VICC_WFEVENTS.SVWF.KS02(this.RUNID);
            }

            base.ManageCommand(sender, e);
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            base.Window_Loaded(sender, e);
        }
        public void ResetSeq(object sender, RoutedEventArgs e)
        {
            if (!TMV.IsITX)
            {
                return;
            } 
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                KEY = "";

            }
            else
            {
                KEY = TMV.KSZKSTKRS + TMV.KSZKST + TMV.KSZAGANG+dwMaster["KDES"].ToString();
            }
        }
        public override void OnSave()
        {
            if (!IsSaved)
                return;
            foreach(DataRow row in TBMain.Rows)
            {
                TMV.UpdateKSTAGZUO(row, 0);
            } 
            base.OnSave();
        }
        public override void LOADBYAUF()
        {
            if (!IsCanLoad)
                return;
            TBMain.Clear();
            if (ORDERNUM == "")
                return;
            dwData = TMV.TBMain.NewRow();
            dwData["KSZMANDANT"] = app.Mandant;
            dwData["KSZKSTKRS"] = "AND KSZKSTKRS= '" + ORDERNUM.Substring(0, 2) + "'";
            dwData["KSZKST"] = "AND KSZKST= '" + ORDERNUM.Substring(2, 4) + "'";
            dwData["KSZAGANG"] = "AND KSZAGANG= '" + ORDERNUM.Substring(6, 15) + "'";
            SResult = TMV.QKSZ02(dwData);
            MergeData(SResult, true);
            base.LOADBYAUF();
        }
        public override void OnLOChg(string lo)
        {
            if (!IsEdit)
                return;
            TMV.KSZLO = lo; 
        }
        public override void OnLBChg(string lb)
        {
            if (!IsEdit)
                return;
            TMV.KSZLB = lb; 
        }
        public override void OnLPChg(string lp)
        {
            if (!IsEdit)
                return;
            TMV.KSZLP = lp; 
        }
        public virtual void OnLIEFNRChg(string value)
        {
            if (!IsEdit)
                return;
            TMV.KSZLIEFNR = value; 
        }

    }
    public partial class UIKS05
    {
        public VKSTAGZUO TMV
        {
            get
            {
                return (VKSTAGZUO)MV;
            }
        }
        #region ==KST===========
        public static readonly DependencyProperty KSTProperty = DependencyProperty.Register("KST",
            typeof(string), typeof(UIKS05),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnKSTChanged));
        public string KST
        {
            get
            {
                return (string)GetValue(KSTProperty);
            }
            set
            {
                SetValue(KSTProperty, value);
             
            }
        }
        private static void OnKSTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIKS05;
            if (src != null)
            {
                src.KST = (string)e.NewValue;
                src.OnKSTChg((string)e.NewValue);
            }
        }
        public virtual void OnKSTChg(string value)
        {
            if(value=="")
            {
                KRS = "";
                LFD = "";
            }
            else
            {
                KRS = value.Substring(0, 2);
                LFD = value.Substring(2, 4);
            }
        }
        #endregion 

        #region ==LIEFNR===========
        public static readonly DependencyProperty LIEFNRProperty = DependencyProperty.Register("LIEFNR",
            typeof(string), typeof(UIKS05),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnLIEFNRChanged));
        public string LIEFNR
        {
            get
            {
                return (string)GetValue(LIEFNRProperty);
            }
            set
            {
                SetValue(LIEFNRProperty, value);
                
            }
        }
        private static void OnLIEFNRChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIKS05;
            if (src != null)
            {
                src.LIEFNR = (string)e.NewValue;
                src.OnLIEFNRChg((string)e.NewValue);
            }
        }
        
        #endregion
    }
}
